import 'package:PollOnLine/ui/home/home_state.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../bean/UserTask.dart';
import '../../../values/MyColors.dart';
import 'task_list_logic.dart';

/// 任务列表
class TaskListPage extends StatefulWidget {
  final String title;
  final Tabs tab;

  const TaskListPage({Key? key, required this.title, required this.tab}) : super(key: key);

  @override
  State<TaskListPage> createState() => _TaskListPageState();
}

class _TaskListPageState extends State<TaskListPage> with AutomaticKeepAliveClientMixin {
  late final String status; // 0全部  1进行中  2截止  3已完成
  @override
  void initState() {
    super.initState();
    status = widget.tab.id.toString();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<TaskListLogic>(
        init: TaskListLogic(status),
        // global: false,
        tag: status,
        builder: (logic) {
          return SmartRefresher(
            controller: logic.refreshController,
            //  允许下拉刷新
            enablePullDown: true,
            // 允许上拉加载
            enablePullUp: true,
            // 刷新
            onRefresh: logic.onRefresh,
            // 加载
            onLoading: logic.onLoading,
            child: ListView.builder(
                padding: const EdgeInsets.only(top: 0),
                itemCount: logic.state.userTask?.content?.length ?? 0,
                itemBuilder: (BuildContext context, int index) {
                  return ListItem(logic.state.userTask!.content![index], logic);
                }),
          );
        });
  }

  Widget ListItem(Content item, TaskListLogic logic) {
    return Container(
      margin: EdgeInsets.only(bottom: 6.r),
      height: 111.5.r,
      child: BrnShadowCard(
        color: Colors.white,
        spreadRadius: 6.r,
        child: Container(
            padding: EdgeInsets.only(top: 11.r, bottom: 14.r, left: 15.r, right: 15.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Flexible(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          BrnTagCustom(
                            tagText: logic.getStatus(item)['tagText'],
                            textColor: logic.getStatus(item)['textColor'],
                            backgroundColor: logic.getStatus(item)['bgColor'],
                            fontSize: 12.r,
                            textPadding: EdgeInsets.symmetric(vertical: 1.r, horizontal: 14.r),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 8.r),
                            child: Text(
                              "${item.endTime}截止",
                              style: TextStyle(color: MyColors.FF333333, fontSize: 12.r),
                            ),
                          ),
                        ],
                      ),
                      Text(
                        "${item.taskName}",
                        overflow: TextOverflow.clip,
                        maxLines: 2,
                        style: TextStyle(color: MyColors.FF313336, fontSize: 15.r, fontWeight: FontWeight.bold),
                      ),
                      Row(
                        children: [
                          Text("调查样本: ${item.writeCount}/${item.collectCount}", style: TextStyle(color: MyColors.FF666666, fontSize: 13.r)),
                          Padding(
                            padding: EdgeInsets.only(left: 11.5.r),
                            child: ClipRRect(
                              borderRadius: BorderRadius.all(Radius.circular(100.r)),
                              child: BrnProgressChart(
                                key: UniqueKey(),
                                width: 63.r,
                                height: 4.r,
                                value: (item.writeCount ?? 0) / (item.collectCount ?? 1),
                                showAnimation: true,
                                colors: [logic.getStatus(item)['proColor'], logic.getStatus(item)['proColor']],
                                backgroundColor: MyColors.FFE5E5E5,
                                brnProgressIndicatorBuilder: (BuildContext context, double value) {
                                  return const Text('');
                                },
                              ),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 3.5.r),
                            child: Text(logic.getPercent(item.writeCount, item.collectCount),
                                style: TextStyle(color: MyColors.FF888888, fontSize: 13.r)),
                          )
                        ],
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(right: 8.r, left: 5.r, top: 2.r, bottom: 2.r),
                  child: VerticalDivider(
                    width: 0.5.r,
                    color: MyColors.FFDCDCDC,
                  ),
                ),
                SizedBox(
                  width: 75.r,
                  height: 30.r,
                  child: item.status == "1"
                      ? BrnNormalButton(
                          text: '继续填写',
                          fontSize: 13.r,
                          textColor: Colors.white,
                          backgroundColor: MyColors.FF1060DD,
                          insertPadding: const EdgeInsets.all(0),
                          alignment: Alignment.center,
                          borderRadius: BorderRadius.circular(4.r),
                          onTap: () {
                            logic.goToNextPoll(item.taskId);
                          },
                        )
                      : BrnNormalButton.outline(
                          text: "查看结果",
                          fontSize: 13.r,
                          textColor: MyColors.FF1060DD,
                          lineColor: MyColors.FF1060DD,
                          backgroundColor: Colors.white,
                          insertPadding: const EdgeInsets.all(0),
                          alignment: Alignment.center,
                          radius: 4.r,
                          onTap: () {
                            logic.goToNextPoll(item.taskId);
                          },
                        ),
                )
              ],
            )),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<TaskListLogic>();
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
